<template>
	<view>
		<view class="shop-detail-container">
			<!-- 商品图片区域 -->
			<view class="shop-detail-header">
				<image class="product-image" :src="config.baseUrl + shopProduct.coverUrl" mode="aspectFill"></image>
				<view class="image-overlay"></view>
			</view>

			<!-- 商品信息卡片 -->
			<view class="product-info">
				<view class="product-name">
					<text class="name-text">{{ shopProduct.productName }}</text>
					<view class="tag">热销</view>
				</view>

				<view class="price-section">
					<text class="price-symbol">¥</text>
					<text class="price-value">{{ shopProduct.price }}</text>
					<!-- <text class="original-price">¥{{ shopProduct.price * 1.2 }}</text> -->
				</view>

				<view class="divider"></view>

				<view class="description">
					<view class="desc-title">商品描述</view>
					<view class="desc-content">{{ shopProduct.description }}</view>
				</view>

				<!-- 商品属性信息 -->
				<view class="product-attrs">
					<view class="attr-item">
						<text class="attr-label">类型</text>
						<text class="attr-value">{{ shopProduct.type }}</text>
					</view>
					<view class="attr-item">
						<text class="attr-label">库存</text>
						<text class="attr-value">{{ shopProduct.stock }}件</text>
					</view>
					<view class="attr-item">
						<text class="attr-label">发货</text>
						<text class="attr-value">24小时内发货</text>
					</view>
					<view class="attr-item">
						<text class="attr-label">运费</text>
						<text class="attr-value">全国包邮</text>
					</view>
				</view>
			</view>

			<!-- 服务保障区域 -->
			<view class="service-section">
				<view class="service-item">
					<text class="iconfont icon-guarantee"></text>
					<text>正品保障</text>
				</view>
				<view class="service-item">
					<text class="iconfont icon-return"></text>
					<text>7天退换</text>
				</view>
				<view class="service-item">
					<text class="iconfont icon-delivery"></text>
					<text>极速发货</text>
				</view>
			</view>

			<!-- 底部购买按钮 -->
			<view class="bottom-actions">
				<view class="action-left">
					<!-- <button class="icon-btn">
						<text class="iconfont icon-cart"></text>
						<text>购物车</text>
					</button> -->
				</view>
				<button class="buy-btn" @tap="handleBuy">立即购买</button>
			</view>
		</view>

		<!-- 添加购买弹窗 -->
		<uni-popup ref="buyPopup" type="bottom">
			<view class="popup-content">
				<view class="popup-header">
					<image class="popup-image" :src="config.baseUrl + shopProduct.coverUrl" mode="aspectFill"></image>
					<view class="popup-info">
						<text class="popup-price">¥{{ shopProduct.price }}</text>
						<text class="popup-stock">库存: {{ shopProduct.stock }}件</text>
					</view>
					<text class="close-btn" @tap="closeBuyPopup">×</text>
				</view>

				<view class="popup-body">
					<view class="quantity-control">
						<text class="quantity-label">购买数量</text>
						<view class="quantity-wrapper">
							<button class="quantity-btn" @tap="decreaseQuantity" :disabled="quantity <= 1">-</button>
							<input class="quantity-input" type="number" v-model="quantity" />
							<button class="quantity-btn" @tap="increaseQuantity" :disabled="quantity >= shopProduct.stock">+</button>
						</view>
					</view>
				</view>

				<view class="popup-footer">
					<text class="total-price">总计：¥{{ (shopProduct.price * quantity).toFixed(2) }}</text>
					<button class="confirm-btn" @tap="confirmBuy">确认购买</button>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
import config from "@/config.js";
import { createOrder } from "@/api/system/shop";
export default {
	data() {
		return {
			config,
			shopProduct: {},
			quantity: 1 // 添加数量变量
		};
	},
	onLoad(options) {
		this.shopProduct = JSON.parse(uni.getStorageSync("shopProduct"));
		console.log(this.shopProduct);
	},
	methods: {
		handleBuy() {
			this.$refs.buyPopup.open();
		},
		closeBuyPopup() {
			this.$refs.buyPopup.close();
		},
		decreaseQuantity() {
			if (this.quantity > 1) {
				this.quantity--;
			}
		},
		increaseQuantity() {
			if (this.quantity < this.shopProduct.stock) {
				this.quantity++;
			}
		},
		confirmBuy() {
			let obj = {
				price: this.shopProduct.price * this.quantity,
				orderProducts: [
					{
						productId: this.shopProduct.id,
						productName: this.shopProduct.productName,
						coverUrl: this.shopProduct.coverUrl,
						num: this.quantity,
						price: this.shopProduct.price
					}
				]
			};

			createOrder(obj).then((res) => {
				if (res.code == 200) {
					this.$refs.buyPopup.close();
					uni.showToast({
						title: "购买成功",
						icon: "success"
					});
				}
			});
		}
	}
};
</script>

<style lang="scss">
.shop-detail-container {
	min-height: 100vh;
	background-color: #f8f8f8;
	padding-bottom: 120rpx;

	.shop-detail-header {
		position: relative;
		width: 100%;
		height: 500rpx;

		.product-image {
			width: 100%;
			height: 100%;
		}

		.image-overlay {
			position: absolute;
			bottom: 0;
			left: 0;
			right: 0;
			height: 60rpx;
			background: linear-gradient(180deg, rgba(255, 255, 255, 0) 0%, rgba(255, 255, 255, 1) 100%);
		}
	}

	.product-info {
		background-color: #fff;
		padding: 40rpx;
		margin: -40rpx 20rpx 20rpx;
		border-radius: 20rpx;
		box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.05);

		.product-name {
			display: flex;
			align-items: center;

			.name-text {
				font-size: 40rpx;
				font-weight: bold;
			}

			.tag {
				margin-left: 20rpx;
				font-size: 24rpx;
				color: #ff4400;
				background: rgba(255, 68, 0, 0.1);
				padding: 4rpx 16rpx;
				border-radius: 20rpx;
			}
		}

		.price-section {
			margin: 30rpx 0;
			display: flex;
			align-items: baseline;

			.price-symbol {
				color: #ff4400;
				font-size: 32rpx;
			}

			.price-value {
				color: #ff4400;
				font-size: 48rpx;
				font-weight: bold;
			}

			.original-price {
				margin-left: 20rpx;
				color: #999;
				font-size: 26rpx;
				text-decoration: line-through;
			}
		}

		.divider {
			height: 2rpx;
			background: #f5f5f5;
			margin: 30rpx 0;
		}

		.desc-title {
			font-size: 32rpx;
			font-weight: bold;
			margin-bottom: 20rpx;
		}

		.desc-content {
			color: #666;
			font-size: 28rpx;
			line-height: 1.6;
		}

		.product-attrs {
			margin-top: 40rpx;
			background: #f9f9f9;
			padding: 20rpx;
			border-radius: 12rpx;

			.attr-item {
				display: flex;
				align-items: center;
				margin-bottom: 20rpx;

				&:last-child {
					margin-bottom: 0;
				}

				.attr-label {
					color: #999;
					font-size: 28rpx;
					margin-right: 40rpx;
					min-width: 120rpx;
				}

				.attr-value {
					color: #333;
					font-size: 28rpx;
				}
			}
		}
	}

	.service-section {
		background: #fff;
		margin: 20rpx;
		padding: 30rpx;
		border-radius: 16rpx;
		display: flex;
		justify-content: space-around;

		.service-item {
			display: flex;
			flex-direction: column;
			align-items: center;
			font-size: 24rpx;
			color: #666;

			.iconfont {
				font-size: 40rpx;
				color: #32b847;
				margin-bottom: 10rpx;
			}
		}
	}

	.bottom-actions {
		position: fixed;
		bottom: 0;
		left: 0;
		right: 0;
		background-color: #fff;
		padding: 20rpx 30rpx;
		box-shadow: 0 -2rpx 10rpx rgba(0, 0, 0, 0.05);
		display: flex;
		align-items: center;

		.action-left {
			display: flex;
			margin-right: 30rpx;

			.icon-btn {
				background: none;
				display: flex;
				flex-direction: column;
				align-items: center;
				margin-right: 40rpx;
				padding: 0;
				line-height: 1.2;

				&::after {
					border: none;
				}

				.iconfont {
					font-size: 40rpx;
					color: #666;
				}

				text {
					font-size: 24rpx;
					color: #999;
				}
			}
		}

		.buy-btn {
			flex: 1;
			background: linear-gradient(to right, #32b847, #2ea841);
			color: #fff;
			border-radius: 40rpx;
			font-size: 32rpx;
			height: 80rpx;
			line-height: 80rpx;
			margin: 0;

			&:active {
				opacity: 0.9;
			}
		}
	}
}

// 添加一些动画效果
.product-image {
	transition: transform 0.3s ease;
	&:active {
		transform: scale(1.02);
	}
}

.buy-btn {
	transition: transform 0.2s ease;
	&:active {
		transform: scale(0.98);
	}
}

/* 添加弹窗样式 */
.popup-content {
	background-color: #fff;
	border-radius: 24rpx 24rpx 0 0;
	padding: 30rpx;

	.popup-header {
		display: flex;
		position: relative;
		padding-bottom: 30rpx;
		border-bottom: 1rpx solid #eee;

		.popup-image {
			width: 180rpx;
			height: 180rpx;
			border-radius: 12rpx;
		}

		.popup-info {
			margin-left: 20rpx;
			display: flex;
			flex-direction: column;
			justify-content: space-around;

			.popup-price {
				color: #ff4400;
				font-size: 36rpx;
				font-weight: bold;
			}

			.popup-stock {
				color: #999;
				font-size: 26rpx;
			}
		}

		.close-btn {
			position: absolute;
			right: 0;
			top: 0;
			font-size: 48rpx;
			color: #999;
			padding: 10rpx;
		}
	}

	.popup-body {
		padding: 30rpx 0;

		.quantity-control {
			display: flex;
			justify-content: space-between;
			align-items: center;

			.quantity-label {
				font-size: 28rpx;
				color: #333;
			}

			.quantity-wrapper {
				display: flex;
				align-items: center;

				.quantity-btn {
					width: 60rpx;
					height: 60rpx;
					padding: 0;
					margin: 0;
					line-height: 60rpx;
					text-align: center;
					background: #f5f5f5;
					border: none;

					&:disabled {
						color: #ccc;
					}
				}

				.quantity-input {
					width: 80rpx;
					height: 60rpx;
					text-align: center;
					margin: 0 10rpx;
					background: #f5f5f5;
				}
			}
		}
	}

	.popup-footer {
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding-top: 30rpx;
		border-top: 1rpx solid #eee;

		.total-price {
			font-size: 32rpx;
			color: #ff4400;
			font-weight: bold;
		}

		.confirm-btn {
			background: linear-gradient(to right, #32b847, #2ea841);
			color: #fff;
			border-radius: 40rpx;
			font-size: 28rpx;
			padding: 0 60rpx;
			height: 80rpx;
			line-height: 80rpx;
		}
	}
}
</style>
